/*********************************************************************************
 *      Copyright:  (C) 2020 YangZhengQing<2489833081@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  epoll_init.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(04/08/2020)
 *         Author:  YangZhengQing <2489833081@qq.com>
 *      ChangeLog:  1, Release initial version on "04/08/2020 02:46:10 PM"
 *                 
 ********************************************************************************/

#include "../include/server.h"

#define FDNUM 20 //epoll实例个数

/*功能:初始化epoll机制
 * 参数:sockfd:传入套接字
 * 返回:成功返回epollfd,失败返回-1*/
int epoll_init(int sockfd)
{
    int                 epollfd = -1;
    int                 rv = -1;
    struct epoll_event  event;

    /* 创建epoll文件描述符，相当于向内核申请一个文件系统*/
    epollfd = epoll_create(FDNUM);
    if(epollfd < 0)
    {
        printf("epoll create error:%s\n",strerror(errno));
        return -1;
    }
    /* 将套接字文件描述符设置为非阻塞*/
   /* if(setnonblocking(sockfd) < 0)
    {
        printf("setnonblocking error:%s\n",strerror(errno));
        close(sockfd);
        exit(0);

    }
*/
    /* 配置sockEvent感性趣的事件*/
    event.events = EPOLLIN ;//设置为读,默认水平触发
    event.data.fd = sockfd;

    /* 注册事件*/
    rv = epoll_ctl(epollfd,EPOLL_CTL_ADD,sockfd,&event);
    if(rv < 0)
    {
        printf("epoll_ctl error:%s\n",strerror(errno));
        close(epollfd);
        return -1;
    }

    return epollfd;
}
